<?
//Restore portion of Sitebackup.php
//list contents of /dump/
extract($_POST);
$url_base = ereg_replace("restore.php", '', "$_SERVER[PATH_TRANSLATED]");
$url_base=str_replace("\\\\","/",$url_base)."dump/";
?>
<HTML>
<HEAD>
<TITLE>PHP Website Backup Helper :: Restore</TITLE>
<STYLE type="text/css">
BODY {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10pt;
}

.textbox {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 8pt;
	background-color: #BDD7F7;
	border: 1px solid #000000;
	color: #000000;
}
</STYLE>
</HEAD>
<BODY BGCOLOR="#F4F4F4">
<CENTER>
  <TABLE WIDTH="80%" bgcolor="#8BA5C5">
    <TR>
      <TD bgcolor="#BDD7F7"><h3><font color="#6699CC">PHP <font color="#FF9900">Website</font> Backup :: Restore</font></h3></TD>
    </TR>
  </table>  
  <TABLE WIDTH="80%" bgcolor="#8BA5C5" border="0" cellspacing="0">

<?
if (!file_exists($url_base)){
   //echo "<tr><td> ERROR: There is no backup directory (/Dump) on the server.</td></tr>";
   $tst=@mkdir($url_base,0777);
   if (!$tst==false) {
   echo "<tr><td><br>The ". $url_base." directory has been recreated. <br>
        Please upload your backup first.<br><br></td></tr>";
   } else {
      echo "<tr><td><br>Something went wrong ". $url_base." has not been recreated.<br><br></td></tr>";      
   }
} else {
   if ($handle = opendir($url_base)) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") { 
           // check for .tar.gz file
           if (false!==strpos($file,".tar.gz")){
             break; //found
           }              
        } 
    }
    closedir($handle); 
}
if (empty($file)) {
   echo "<tr><td> ERROR: There is no backup on the server.<br>Only an empty /dump directory.<br><br></td></tr>    <TR><TD bgcolor='#FFFFFF' align=right>
        <font color='#6699CC' face='Arial, Helvetica, sans-serif' style='font-size:6Pt'>
        Php Website Backup Version 1.1 &copy; 2004 by <a href='http://www.absoft-my.com' target='_blank'>AB 
        Webservices</a></font> </TD>
    </TR>
  </TABLE>";
  die();
}

$file1=str_replace("tar.gz","tar",$file);

$zp = @gzopen($url_base.$file, "rb"); 
@unlink($url_base.$file1);
$fp2 = @fopen($url_base.$file1,"wb");
fwrite ($fp2,"");
fclose ($fp2);
chmod($url_base.$file1, 0777);
$fp = @fopen($url_base.$file1,"wb");

if(!$fp) {
   die("<tr><td>No tar file can be created! <br><br></td></tr></table>"); 
}    
if(!$zp) {
   die("<tr><td>Cannot read zip file! <br><br></td></tr></table>");
}    
while(!gzeof($zp)){
   $data=gzgets($zp, 8192);// buffer php
   fwrite($fp,$data);
}
fclose($fp);
gzclose($zp);
//now we have a tar file

  
include("Tar.php");
// listing the filecontents
if (!isset($dorestore)) {
    echo "
    <TR> 
      <TD valign='top'> 
        <ul>
          <li> 
            <div align='left'><font color='#66FFFF' size='1' face='Verdana, Arial, Helvetica, sans-serif'>
            Pear.php must be available.<br>
            The following files have been found in the backupfile:<br>
            <font size=2 color=black>".$file."</font>
            </font></div>
          </li>
        </ul>
    </TD></TR>
    <TR><TD>
    <Form name='restore' METHOD='post' ACTION='restore.php' style='display:inline'>
    <table width='80%' align=center border='0' cellspacing='0' cellpadding='0'>
    <tr>
       <td width='9'% align=right></td><td align=left>Filename</td><td width='15%'>File Size</td><td width='26%' align=center>Date</td><td width='3%'></td>
    </tr>";

    $tar_object = new Archive_Tar($url_base.$file1);
    if (($v_list  =  $tar_object->listContent()) != 0) {
       sort($v_list);
       reset($v_list);
       $d=0;
       for ($i=0; $i<sizeof($v_list); $i++) {
          if ($d==1) {
             $bgc='#BDD7F7';
             $d=0;
          } else {
             $bgc='#B6D2F2';
             $d=1;
          }   

          echo "<tr>
                  <td align=right bgcolor=\"$bgc\"><font size=\"1\">".($i+1)." <input type=\"checkbox\" name='select".$i."'></font></td>
                  <td bgcolor=\"$bgc\"><font size=1>../".$v_list[$i][filename]."</font></td>
                  <td bgcolor=\"$bgc\"><font size=\"1\">".$v_list[$i][size]."</td>
                  <td bgcolor=\"$bgc\"><font size=1>".date("d-m-Y h:i:s", $v_list[$i][mtime])."</font></td>
                  <td bgcolor=\"$bgc\"><input type=\"hidden\" name='rfile".$i."' value='".$v_list[$i][filename]."'></td></tr>";
       }
    }
    echo "<tr><td></td><td colspan='4'>
      <input type='radio' name='chg' onclick='for(d=0;d<$i;d++){document.getElementById(\"select\"+d).checked=true;}'><FONT SIZE='1' FACE='verdana,sans-serif'> Check All  &nbsp;&nbsp;</font>
      <input type='radio' name='chg' onclick='for(d=0;d<$i;d++){document.getElementById(\"select\"+d).checked=false;}'><FONT SIZE='1' FACE='verdana,sans-serif'> Uncheck All &nbsp;&nbsp;</font>
      <br><br></td></tr>";

    // extract confirmation
    echo "<tr><td align=center colspan=5><font size=2 color=red>
    Warning: Pressing RESTORE means ALL selected files in the backup will<br>
    overwrite the same files on the server! <b>No further warning.</B></center></font></td></tr>";
    ?>
    </td></tr><tr><td align=center colspan=5><br>
    <input type="submit" name="dorestore" value="Restore" class="textbox"><br>
    <br></td></tr></table></form>
<?
}
if (isset($dorestore)){

  //extracting:
  // ----- Extracting the archive in directory extract()

  // build array
  $sel_file= array();
  $c=0;
  $d=0;
  do {
       // check whether selected
       $chk  ='select'.$c;
       $rf   ='rfile'.$c;
       if (!isset($$rf)) {
          break;
       }   
       if ($$chk=='on') {
          $sel_file[$d]=$$rf; 
          $d++;
       }
       $c++;
   }  while(isset($$rf));       
   
   if ( $d >=1 ) {
      // start restoring
      include_once("Tar.php");
      $tar_object = new Archive_Tar($url_base.$file1);
      $tar_object->setErrorHandling(PEAR_ERROR_PRINT);

      $tar_object->extractList($sel_file,substr($_SERVER[DOCUMENT_ROOT],0,strlen($_SERVER[DOCUMENT_ROOT])-1));
      // end
      echo "<tr><td><br>End of restore. ".$d." files restored.<br>
            <font size=1>If no error messages were displayed all is restored ok.</font><br><br></td></tr>
            <tr><td>Please use the delete portion of sitebackup to remove the /dump directory + data<br>
            Click <a href='websitebackup.php'>here</a> to go there now</td</tr>";
   } else {
      echo "<tr><td>NO file(s) selected for restoring.<br><br></tr></td>";
   }      
}}
?>





    <TR><TD bgcolor="#FFFFFF" align=right>
        <font color="#6699CC" face="Arial, Helvetica, sans-serif" style="font-size:6Pt">
        Php Website Backup Version 1.1 &copy; 2004 by <a href="http://www.absoft-my.com" target="_blank">AB 
        Webservices</a></font> </TD>
    </TR>
  </TABLE>